﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace BugzX.Services
{
    /// <summary>
    /// Summary description for Projects
    /// </summary>
    public class Projects : IHttpHandler
    {
        public BugzXDBEntities DBContext
        {
            get { return _dbContext; }
            set { _dbContext = value; }
        }
        private BugzXDBEntities _dbContext = new BugzXDBEntities();

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.Params["action"].Trim().ToLower();

            object response = null;

            if (action == "getall")
            {
                response = this.GetAll(context);
            }

            context.Response.ContentType = "application/json";
            context.Response.Write(new JavaScriptSerializer().Serialize(response));

            context.Response.Flush();
            context.Response.End();
        }

        private object GetAll(HttpContext context)
        {
            return from p in this.DBContext.Projects
                   select new
                   {
                       projectId = p.ProjectId,
                       name = p.Name,
                       ownerId = p.OwnerId,
                       logoUrl = p.LogoUrl,
                   };
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}